package com.study.config;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import com.study.filter.LoginUserInfoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * web mvc 配置
 */

@Configuration
public class WebMvcConfig  extends WebMvcConfigurationSupport{

    /**
     * 添加拦截器配置
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new LoginUserInfoInterceptor())
                .addPathPatterns("/**").order(0);

        //seata 传递xid 事务id给其他微服务
        //其他服务才会写undo_log,才能够实现回滚
        registry.addInterceptor(new SeataHandlerInterceptor())
                .addPathPatterns("/**");
    }

    /**
     * 让 mvc 加载 swagger 静态资源
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        super.addResourceHandlers(registry);
    }
}
